/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.classdiagram.parsers.modelgenerator;

import java.io.StringReader;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import antlr.RecognitionException;
import antlr.TokenStreamException;

import com.cea.papyrus.classdiagram.parsers.PapyrusParsersPlugin;
import com.cea.papyrus.classdiagram.parsers.antlr.ParameterLabelLexer;
import com.cea.papyrus.classdiagram.parsers.antlr.ParameterLabelParser;
import com.cea.papyrus.umlutils.Parameter;

/**
 * 
 * 
 * @author Remi SCHNEKENBURGER
 * CEA-List
 */
public class ParameterGenerator {
	
	/**
	 * 
	 */
	private Parameter parameter;
	
	/**
	 * Default Constructor.
	 * 
	 * @param parameter the parameter to modify
	 */
	public ParameterGenerator(Parameter parameter) {
		this.parameter = parameter;
	}
	
	/**
	 * Parse the label of the parameter and modify parameter attributes.
	 * @param label the label that defines the parameter
	 */
	public void parseAndModifyParameter(String label) {
		try {
			ParameterLabelLexer lexer = new ParameterLabelLexer(new StringReader(label));
	    	ParameterLabelParser parser = new ParameterLabelParser(lexer, parameter);
	    	parser.label();
			
		} catch (RecognitionException e){
			PapyrusParsersPlugin.getDefault().getLog().log(
					new Status(IStatus.ERROR,
							PapyrusParsersPlugin.PLUGIN_ID,
							IStatus.OK,
							"could not parse the parameter label: "+label,
							e));
		} catch (TokenStreamException e){
			PapyrusParsersPlugin.getDefault().getLog().log(
					new Status(IStatus.ERROR,
							PapyrusParsersPlugin.PLUGIN_ID,
							IStatus.OK,
							"could not parse the parameter label: "+label,
							e));
		}
    }
	
	/**
	 * Parse the label of the parameter and validate it.
	 * @param label the label that defines the parameter
	 * @return null if label is valid, else return the message that describes the error.
	 */
	public String parseAndValidateParameter(String label) {
		String message = null;
		
		// to validate the message : parse it. If no errors, it is ok. If exceptions : not ok. Returns the exception message
		ParameterLabelLexer lexer = new ParameterLabelLexer(new StringReader(label));
    	ParameterLabelParser parser = new ParameterLabelParser(lexer, parameter);
    	parser.setValidation(true);
    	try {
			parser.label();
		} catch (RecognitionException e) {
			message = e.getLocalizedMessage();
		} catch (TokenStreamException e) {
			message = e.getLocalizedMessage();
		}
		
		return message;
	}
}
